<template>
  <div class="m-content">
    <h3>欢迎来到MarkerHub的博客</h3>
    <span v-if="hasLogin">
       <el-avatar :size="50" :src="user.avatar"></el-avatar>
       <div>{{ user.username }}</div>
    </span>
    <span v-if="!hasLogin">
      <el-link type="primary" href="/login">
       <el-avatar :size="50" :src="user.avatar"></el-avatar>
       <div>{{ user.username }}</div>
      </el-link>
    </span>

    <div class="maction">
      <!--      <el-link href="/blogs" target="_blank">主页</el-link>-->

      <el-link type="primary">
        <router-link :to="{name: 'Blogs'}">主页</router-link>
      </el-link>

      <el-divider direction="vertical"></el-divider>
      <el-link type="success" href="/blog/add" :disabled="!hasLogin">发表文章</el-link>
      <span v-if="!hasLogin">
        <el-divider direction="vertical"></el-divider>
        <el-link type="primary" href="/login">登录</el-link>
      </span>
      <span v-if="hasLogin">
        <el-divider direction="vertical"></el-divider>
        <el-link type="danger" @click="logout">退出</el-link>
      </span>
    </div>
  </div>
</template>

<script>
export default {
  name: "Header",
  data() {
    return {
      user: {
        username: '请先登录',
        avatar: 'https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png'
      },
      hasLogin: false,
      isRouterAlive: true
    }
  },
  methods: {
    logout() {
      const _this = this
      this.$axios.post('/logout', null, {}).then(result => {
            _this.$store.commit('REMOVE_INFO')
            _this.$router.push('/login')
            // _this.$router.push('/blogs')
            // _this.$nextTick(() => this.isRouterAlive = true)
          }
      )
    }
  },
  created() {
    if (this.$store.getters.getUser) {
      const userInfo = this.$store.getters.getUser
      this.user.username = userInfo.username
      this.user.avatar = userInfo.avatar

      this.hasLogin = true
    }
  }
}
</script>

<style scoped>
.m-content {
  max-width: 960px;
  margin: 0 auto;
  text-align: center;
}

.maction {
  margin: 10px;
}
</style>
